home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1997 December
/
MACPOWER-1997-12.ISO.7z
/
MACPOWER-1997-12.ISO
/
AMUG
/
PROGRAMMING
/
Raven 1.2.sit
/
Raven 1.2
/
Source
/
Foundation
/
Common
/
ZNumbers.h
< prev
next >
Wrap
Text File
|
1997-08-17
|
5KB
|
121 lines
/*
* File: ZNumbers.h
* Summary: Number related functions.
* Written by: Jesse Jones
*
* Copyright ゥ 1996-1997 Jesse Jones.
* For conditions of distribution and use, see copyright notice in ZTypes.h
*
* Change History (most recent first):
*
* <6> 8/18/97 JDJ Removed Sqrt template function.
* <5> 6/20/97 JDJ Abs() functions use intrinsics on PPC.
* <4> 4/27/97 JDJ Added int and double versions of Random.
* <3> 4/15/97 JDJ Renamed EqualReal Equal. Reformatted.
* <2> 3/30/97 JDJ Added 3 and 4 argument versions of Min and Max.
* <1> 1/13/96 JDJ Created
*/
#pragma once
#include <ZTypes.h>
//-----------------------------------
// Swap, Min, Max
//
template <class T> inline void Swap(T& x, T& y) {T z = x; x = y; y = z;}
template <class T> inline T Min(const T& x, const T& y) {return x < y ? x : y;}
template <class T> inline T Max(const T& x, const T& y) {return x > y ? x : y;}
template <class T> inline T MinMax(const T& lo, const T& x, const T& hi) {return Min(Max(x, lo), hi);}
// Returns x pinned to lo and high.
template <class T> inline T Min(const T& x, const T& y, const T& z) {return Min(Min(x, y), z);}
template <class T> inline T Min(const T& x, const T& y, const T& z, const T& w) {return Min(Min(Min(x, y), z), w);}
template <class T> inline T Max(const T& x, const T& y, const T& z) {return Max(Max(x, y), z);}
template <class T> inline T Max(const T& x, const T& y, const T& z, const T& w) {return Max(Max(Max(x, y), z), w);}
//-----------------------------------
// Sign
//
template <class T> inline short Sign(T value) {return value > 0 ? 1 : ((value < 0) ? -1 : 0);}
// Returns -1, 0, or +1.
//-----------------------------------
// Random
//
int Random(int max);
double Random(double max);
long Random(long max);
// Returns a random number in [0, max).
int Random(int min, int max);
long Random(long min, long max);
double Random(double min, double max);
// Returns a random number in [min, max).
bool FlipCoin();
// Very fast heads or tails random number generator.
void SetRandomSeed(long seed);
// Note that the seed is inited to a psuedo-random value.
//-----------------------------------
// Misc
//
inline bool InBounds(long lo, long value, long hi) {return (lo <= value) && (value <= hi);}
// Returns true if lo <= value <= hi.
inline short MulDiv(short value, short num, short denom) {return (short) ((long) value*num/denom);}
inline int MulDiv(int value, int num, int denom) {return value*num/denom;}
inline ulong MulDiv(ulong value, ulong num, ulong denom) {return value*num/denom;}
// Returns value*num/denom. Uses longs to avoid overflows.
bool Equal(double x, double y, double tolerance = 1.0e-6, double typical = 1.0);
// Returns true if the difference between the numbers is less
// than tolerance or the numbers are much smaller than typical.
inline bool Equal(short x, short y) {return x == y;}
inline bool Equal(int x, int y) {return x == y;}
inline bool Equal(long x, long y) {return x == y;}
inline bool Even(long x) {return (x & 1) == 0;}
inline bool Even(ulong x) {return (x & 1) == 0;}
inline bool Odd(long x) {return (x & 1) == 1;}
inline bool Odd(ulong x) {return (x & 1) == 1;}
// ===================================================================================
// Inlines
// ===================================================================================
#if powerc
inline short Abs(short value) {return (short) __abs(value);}
inline int Abs(int value) {return (int) __abs(value);}
inline long Abs(long value) {return __labs(value);}
inline float Abs(float value) {return (float) __fabs(value);}
inline double Abs(double value) {return __fabs(value);}
#else
inline short Abs(short value) {return value < 0 ? -value : value;}
inline int Abs(int value) {return value < 0 ? -value : value;}
inline long Abs(long value) {return value < 0 ? -value : value;}
inline float Abs(float value) {return value < 0 ? -value : value;}
inline double Abs(double value) {return value < 0 ? -value : value;}
#endif
inline short Random(short max) {return (short) Random((long) max);}
inline ushort Random(ushort max) {return (ushort) Random((long) max);}
inline int Random(int max) {return Random((long) max);}
inline int Random(int min, int max) {return Random((long) min, (long) max);}